const { DataTypes } = require('sequelize');

module.exports = (sequelize) => {
    const Order = sequelize.define('RecycleOrder', {
        id: {
            type: DataTypes.BIGINT,
            primaryKey: true,
            autoIncrement: true,
            comment: '订单ID'
        },
        order_no: {
            type: DataTypes.STRING(32),
            allowNull: false,
            unique: true,
            comment: '订单编号'
        },
        user_id: {
            type: DataTypes.BIGINT,
            allowNull: false,
            references: {
                model: 'users',
                key: 'id'
            }
        },
        address: {
            type: DataTypes.TEXT,
            allowNull: false
        },
        contact_name: {
            type: DataTypes.STRING(50),
            allowNull: false
        },
        contact_phone: {
            type: DataTypes.STRING(20),
            allowNull: false
        },
        total_weight: DataTypes.DECIMAL(10, 2),
        total_points: DataTypes.INTEGER,
        status: {
            type: DataTypes.TINYINT,
            defaultValue: 0,
            comment: '0-待接单 1-已接单 2-回收中 3-已完成 4-已取消'
        },
        remark: DataTypes.TEXT,
        deleted_at: DataTypes.DATE
    }, {
        paranoid: true,
        timestamps: true,
        tableName: 'recycle_orders',
        comment: '回收订单表'
    });

    Order.associate = function (models) {
        Order.belongsTo(models.User, {
            foreignKey: 'user_id',
            as: 'user'
        });
        Order.hasMany(models.OrderItem, {
            foreignKey: 'order_id',
            as: 'items'
        });
    };

    return Order;
};